// 第一种方法封装axios
// import axios from 'axios'
// // 列表请求
// function httpForList () {
//   return axios({
//     url: 'https://m.maizuo.com/gateway?cityId=440300&pageNum=1&pageSize=20&type=1&k=7001419',
//     headers: {
//       'X-Client-Info':
//             '{"a":"3000","ch":"1002","v":"5.2.0","e":"16469897096836900740464641","bc":"310100"}',
//       'X-Host': 'mall.film-ticket.film.list'
//     }
//   })
// }
// // 详情请求
// function httpForInfo (params) {
//   return axios({
//     url: `https://m.maizuo.com/gateway?filmId=${params}&k=891498`,
//     headers: {
//       'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16469897096836900740464641","bc":"440300"}',
//       'X-Host': 'mall.film-ticket.film.info'
//     }
//   })
// }
// export default {
//   httpForList,
//   httpForInfo
// }

// 第二种方法封装axios，axios自带的封装方法
import axios from 'axios'
import { Toast } from 'vant'

const http = axios.create({
  baseURL: 'https://m.maizuo.com',
  timeout: 10000,
  headers: {
    'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16469897096836900740464641","bc":"440300"}'
  }
})

http.interceptors.request.use(function (config) { // axios发送请求前进行拦截--showloading
  // Do something before request is sent
  // 在发送请求之前做些什么
  Toast.loading({
    message: '加载中...',
    forbidClick: true
  })
  return config
}, function (error) {
  // Do something with request error
  // 处理请求错误
  return Promise.reject(error)
})

// Add a response interceptor
http.interceptors.response.use(function (response) { // axios请求成功之前进行拦截 -- hideloading
  // Any status code that lie within the range of 2xx cause this function to trigger
  // 任何在2xx范围内的状态代码都会触发此功能
  // Do something with response data
  // 处理响应数据
  Toast.clear()
  return response
}, function (error) { // axios 请求失败之前进行拦截
  // Any status codes that falls outside the range of 2xx cause this function to trigger
  // 任何超出2xx范围的状态代码都会触发此功能
  // Do something with response error
  // 处理响应错误
  return Promise.reject(error)
})

export default http
